package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.DishFlavorsMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        /**
         * 插入菜品数据
         */
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);

        /**
         * 获取菜品对应的口味
         */
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            /**
             * 插入口味数据
             */
            dishFlavorsMapper.insertBatch(flavors);
        }
        //删除redis缓存
        deleteDishCache();
    }

    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
            List<Dish> onSales = dishMapper.selectStatusByCategoryId(ids);
            if (!CollectionUtils.isEmpty(onSales)){
                throw new BaseException("菜品正在启售中，不能删除");
            }
        dishMapper.deleteByIds(ids);
        dishFlavorsMapper.deleteByDishIds(ids);
        deleteDishCache();
        }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        return dishMapper.getById(id);
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        log.info("修改菜品：{}",dishDTO);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //修改口味
        Long id = dish.getId();
        //先删除旧数据
        dishFlavorsMapper.deleteByDishIds(Collections.singletonList(id));
        //插入新数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorsMapper.insertBatch(flavors);
        }
        deleteDishCache();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> selectStatusByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品：{}",categoryId);
       return dishMapper.selectList(categoryId);
    }

    /**
     * 删除redis缓存
     */
    private void deleteDishCache(){
        Set keys = redisTemplate.keys(RedisConstant.CATEGORY_CACHE_PREFIX + "*");
        redisTemplate.delete(keys);
    }

}
